<template>
  <div class="wrapper">
    <div class="content">
      <div class="form sign-in">
        <h2>登&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;录</h2>
        <el-form :rules="rules" :model="form" ref="loginForm" style="margin-top: 30px">
          <el-form-item prop="account" style="margin-left: 80px; width: 430px">
            <el-input placeholder="请输入账号" size="medium" style="margin: 10px 0;" prefix-icon="el-icon-user" v-model="form.account"></el-input>
          </el-form-item>
          <el-form-item prop="password" style="margin-left: 80px; width: 430px">
            <el-input placeholder="请输入密码" size="medium" style="margin: 10px 0" prefix-icon="el-icon-lock" show-password v-model="form.password"></el-input>
          </el-form-item>
        </el-form>
        <br>
        <a href="#" style="text-decoration: none; margin-left: 150px" >手机验证码登录</a>
        <p class="forgot-pass" style="font-size: 14px; margin-top: -20px"><a href="#" style="text-decoration: none">忘记密码？</a></p>
        <button type="button" class="submit" @click="login">登 录</button>
        <hr color="#D4AF7A">
        <br>
        <font color="chocolate"><h5><center>第三方登录</center></h5></font>
        <center>
          <a href=" "><img style="margin:30px ;" src = "../assets/Login/QQ.png" width="50" height="50"></a>
          <a href=" "><img style="margin:30px ;" src = "../assets/Login/Wechat.png" width="50" height="50"></a>
          <a href=" "><img style="margin:30px ;" src = "../assets/Login/Weibo.png" width="50" height="50"></a>
        </center>
      </div>
      <div class="sub-cont">
        <div class="img">
          <div class="img__text m--up">
            <h2>还未注册？</h2>
            <p>立即注册，一起畅游世界！</p>
          </div>
          <div class="img__text m--in">
            <h2>已有帐号？</h2>
            <p>有帐号就登录吧，好久不见了！</p>
          </div>
          <div class="img__btn">
            <span class="m--up">注 册</span>
            <span class="m--in">登 录</span>
          </div>
        </div>
        <div class="form sign-up">
          <h2>注册</h2>

          <el-form :rules="rules" :model="form" ref="registerForm" style="margin-top: 30px">
            <el-form-item prop="account" style="margin-left: 80px; width: 430px">
              <el-input placeholder="请输入账号" size="medium" style="margin: 10px 0;" prefix-icon="el-icon-user" v-model="form.account"></el-input>
            </el-form-item>
            <el-form-item prop="password" style="margin-left: 80px; width: 430px">
              <el-input placeholder="请输入密码" size="medium" style="margin: 10px 0" prefix-icon="el-icon-lock" show-password v-model="form.password"></el-input>
            </el-form-item>
            <el-form-item prop="verify_password" style="margin-left: 80px; width: 430px">
              <el-input placeholder="请再次输入密码" size="medium" style="margin: 10px 0" prefix-icon="el-icon-lock" show-password v-model="form.verify_password"></el-input>
            </el-form-item>
          </el-form>



          <button type="button" class="submit" @click="register">注 册</button>
          <font color="chocolate"><h5><center>使用第三方账号登录</center></h5></font>
          <center>
            <a href=" "><img style="margin:30px ;" src = "../assets/Login/QQ.png" width="50" height="50"></a>
            <a href=" "><img style="margin:30px ;" src = "../assets/Login/Wechat.png" width="50" height="50"></a>
            <a href=" "><img style="margin:30px ;" src = "../assets/Login/Weibo.png" width="50" height="50"></a>
          </center>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "Login",
  data(){
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.form.password) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return{
      form: {},
      rules: {
        account: [
          {required: true, message: "请输入账号", trigger: "blur"},
          {min: 4, max: 16, message: "长度在4到16个字符", trigger: "blur"}
        ],
        password: [
          {required: true, message: "请输入密码", trigger: "blur"},
          {min: 6, max: 16, message: "长度在6到16个字符", trigger: "blur"}
        ],
        verify_password: [
          {required: true, message: "请输入密码", trigger: "blur"},
          {min: 6, max: 16, message: "长度在6到16个字符", trigger: "blur"},
          {validator: validatePass, trigger: 'blur', required: true}
        ]
      }
    }
  },
  methods:{
      getUserInfo: function () {
        // 调用获取用户信息接口
        var url = axios.defaults.baseURL + ":8000/member/user/info/me"
        axios.get(url).then(res => {
          if (res.data.code == 200){
            var user = res.data.data;
            // 设置 用户信息
            localStorage.setItem("user", JSON.stringify(user));
          }else{
            this.$message.error("网络异常，用户信息获取失败")
          }
        })
      },
      login: function () {
        this.$refs['loginForm'].validate((valid) => {
          if (valid) {
            var url = axios.defaults.baseURL + '/member/user/login/username'
            var loginForm = {
              "username": this.form.account,
              "password": this.form.password
            }
            axios.post(url, loginForm).then(res => {
              if (res.data.code == 200) {
                this.$message.success("登录成功");
                this.$router.push("/home")
                // 设置 token信息
                localStorage.setItem("token", JSON.stringify(res.data.jwt));

                localStorage.setItem("nickName", "测试账号")


                // 获取 用户信息
                this.getUserInfo()
              } else {
                this.$message.error(res.data.msg);
              }
            })
          } else {
            this.$message.error("账号密码不符合规范")
            return false;
          }
        });
      },
      register: function () {
        this.$refs['registerForm'].validate((valid) => {
          if (valid) {
            var registerForm = {
              "username": this.form.account,
              "password": this.form.password
            }
            // 注册接口
            var url = axios.defaults.baseURL+":8000/member/user/register";
            console.log(url);
            axios.post(url, registerForm).then(res => {
              console.log(res)
              if (res.data.code == 200) {
                this.$message.success("注册成功, 将自动返回登录界面");
                this.$router.go(0)
              } else {
                this.$message.error(res.data.msg);
              }
            })
          } else {
            this.$message.error("账号密码不符合规范")
            return false;
          }
        });
      }
  },
  mounted() {
    document.querySelector('.img__btn').addEventListener('click', function() {
      document.querySelector('.content').classList.toggle('s--signup')
    })
  }
}
</script>

<style scoped>
*, *:before, *:after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

.wrapper{
  height: 100vh;
  font-family: 'Open Sans', Helvetica, Arial, sans-serif;
  background-repeat: no-repeat;
  background-image: url("../assets/Login/wybg.jpg");
  background-size:100% 100%;
  overflow: hidden;
  background-attachment:fixed;
}

input, button {
  border: none;
  outline: none;
  background: none;
  font-family: 'Open Sans', Helvetica, Arial, sans-serif;
}

.tip {
  font-size: 20px;
  margin: 40px auto 50px;
  text-align: center;
}

.content {
  overflow: hidden;
  position: absolute;
  left: 50%;
  top: 50%;
  width: 900px;
  height: 550px;
  margin: -300px 0 0 -450px;
  background: #fff;
}

.form {
  position: relative;
  width: 640px;
  height: 100%;
  transition: -webkit-transform 0.6s ease-in-out;
  transition: transform 0.6s ease-in-out;
  transition: transform 0.6s ease-in-out, -webkit-transform 0.6s ease-in-out;
  padding: 50px 30px 0;
}

.sub-cont {
  overflow: hidden;
  position: absolute;
  left: 640px;
  top: 0;
  width: 900px;
  height: 100%;
  padding-left: 260px;
  background: #fff;
  transition: -webkit-transform 0.6s ease-in-out;
  transition: transform 0.6s ease-in-out;
  transition: transform 0.6s ease-in-out, -webkit-transform 0.6s ease-in-out;
}

.content.s--signup .sub-cont {
  -webkit-transform: translate3d(-640px, 0, 0);
  transform: translate3d(-640px, 0, 0);
}

button {
  display: block;
  margin: 0 auto;
  width: 260px;
  height: 36px;
  border-radius: 30px;
  color: #fff;
  font-size: 15px;
  cursor: pointer;
}

.img {
  overflow: hidden;
  z-index: 2;
  position: absolute;
  left: 0;
  top: 0;
  width: 260px;
  height: 100%;
  padding-top: 360px;
}

.img:before {
  content: '';
  position: absolute;
  right: 0;
  top: 0;
  width: 900px;
  height: 100%;
  background-image: url(../assets/Login/bg.jpg);
  background-size: cover;
  transition: -webkit-transform 0.6s ease-in-out;
  transition: transform 0.6s ease-in-out;
  transition: transform 0.6s ease-in-out, -webkit-transform 0.6s ease-in-out;
}

.img:after {
  content: '';
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.6);
}

.content.s--signup .img:before {
  -webkit-transform: translate3d(640px, 0, 0);
  transform: translate3d(640px, 0, 0);
}

.img__text {
  z-index: 2;
  position: absolute;
  left: 0;
  top: 50px;
  width: 100%;
  padding: 0 20px;
  text-align: center;
  color: #fff;
  transition: -webkit-transform 0.6s ease-in-out;
  transition: transform 0.6s ease-in-out;
  transition: transform 0.6s ease-in-out, -webkit-transform 0.6s ease-in-out;
}

.img__text h2 {
  margin-bottom: 10px;
  font-weight: normal;
}

.img__text p {
  font-size: 14px;
  line-height: 1.5;
}

.content.s--signup .img__text.m--up {
  -webkit-transform: translateX(520px);
  transform: translateX(520px);
}
.img__text.m--in {
  -webkit-transform: translateX(-520px);
  transform: translateX(-520px);
}

.content.s--signup .img__text.m--in {
  -webkit-transform: translateX(0);
  transform: translateX(0);
}

.img__btn {
  overflow: hidden;
  z-index: 2;
  position: relative;
  width: 100px;
  height: 36px;
  margin: 0 auto;
  background: transparent;
  color: #fff;
  text-transform: uppercase;
  font-size: 15px;
  cursor: pointer;
}
.img__btn:after {
  content: '';
  z-index: 2;
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  border: 2px solid #fff;
  border-radius: 30px;
}

.img__btn span {
  position: absolute;
  left: 0;
  top: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 100%;
  transition: -webkit-transform 0.6s;
  transition: transform 0.6s;
  transition: transform 0.6s, -webkit-transform 0.6s;
}

.img__btn span.m--in {
  -webkit-transform: translateY(-72px);
  transform: translateY(-72px);
}

.content.s--signup .img__btn span.m--in {
  -webkit-transform: translateY(0);
  transform: translateY(0);
}

.content.s--signup .img__btn span.m--up {
  -webkit-transform: translateY(72px);
  transform: translateY(72px);
}

h2 {
  width: 100%;
  font-size: 26px;
  text-align: center;
}

label {
  display: block;
  width: 260px;
  margin: 25px auto 0;
  text-align: center;
}

label span {
  font-size: 14px;
  color: #909399;
  text-transform: uppercase;
}

input {
  display: block;
  width: 100%;
  margin-top: 5px;
  padding-bottom: 5px;
  font-size: 16px;
  border-bottom: 1px solid rgba(0, 0, 0, 0.4);
  text-align: center;
}

.forgot-pass {
  margin-top: 15px;
  margin-right: 70px;
  text-align: right;
  font-size: 13px;
  color: #cfcfcf;
}

.forgot-pass a {
  color: #cfcfcf;
}

.submit {
  margin-top: 40px;
  margin-bottom: 20px;
  background: #d4af7a;
  text-transform: uppercase;
}

.fb-btn {
  border: 2px solid #d3dae9;
  color: #8fa1c7;
}
.fb-btn span {
  font-weight: bold;
  color: #455a81;
}

.sign-in {
  transition-timing-function: ease-out;
}
.content.s--signup .sign-in {
  transition-timing-function: ease-in-out;
  transition-duration: 0.6s;
  -webkit-transform: translate3d(640px, 0, 0);
  transform: translate3d(640px, 0, 0);
}

.sign-up {
  -webkit-transform: translate3d(-900px, 0, 0);
  transform: translate3d(-900px, 0, 0);
}
.content.s--signup .sign-up {
  -webkit-transform: translate3d(0, 0, 0);
  transform: translate3d(0, 0, 0);
}
</style>